<template>
  <page-layout :show-back="true">
	<view class="received-likes-page">
		
		<!-- 获赞内容列表 -->
		<scroll-view scroll-y class="likes-list" @scrolltolower="loadMore" refresher-enabled 
			:refresher-triggered="isRefreshing" @refresherrefresh="onRefresh">
			<view v-if="receivedLikesList.length > 0" class="list-content">
				<view v-for="(item, index) in receivedLikesList" :key="index" class="like-item">
					<view class="like-header">
						<view class="user-info" @tap="goToUserProfile(item.user.id)">
							<image :src="item.user.avatar" mode="aspectFill" class="user-avatar"></image>
							<view class="user-detail">
								<text class="user-name">{{item.user.nickname}}</text>
								<text class="like-time">{{item.likeTime}}</text>
							</view>
						</view>
					</view>
					<view class="post-content" @tap="goToPostDetail(item.post.id)">
						<view class="post-info">
							<text class="like-desc">赞了你的帖子</text>
							<text class="post-title">{{item.post.title || '无标题'}}</text>
							<text class="post-brief">{{item.post.content}}</text>
						</view>
						<image v-if="item.post.cover" :src="item.post.cover" mode="aspectFill" class="post-cover"></image>
					</view>
				</view>
			</view>
			
			<!-- 空状态 -->
			<view v-if="receivedLikesList.length === 0 && !isLoading" class="empty-state">
				<uni-icons type="heart-filled" size="80" color="#eeeeee"></uni-icons>
				<text class="empty-text">还没有人给你点赞</text>
				<button class="create-btn" @tap="goToCreatePost">去发布内容</button>
			</view>
			
			<!-- 加载状态 -->
			<view v-if="isLoading" class="loading-more">
				<text>加载中...</text>
			</view>
			<view v-if="!isLoading && receivedLikesList.length > 0 && !hasMore" class="no-more">
				<text>没有更多了</text>
			</view>
		</scroll-view>
	</view>
  </page-layout>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import uniIcons from '@dcloudio/uni-ui/lib/uni-icons/uni-icons.vue';
import PageLayout from "../../components/PageLayout.vue";

// 页面数据
const receivedLikesList = ref([]);
const page = ref(1);
const pageSize = ref(10);
const hasMore = ref(true);
const isLoading = ref(true);
const isRefreshing = ref(false);

// 加载获赞列表
const loadReceivedLikesList = async (isRefresh = false) => {
	if (isRefresh) {
		page.value = 1;
		hasMore.value = true;
		receivedLikesList.value = [];
	}
	
	isLoading.value = true;
	
	// 模拟请求延迟
	setTimeout(() => {
		// 模拟接口返回数据 - 别人点赞我的帖子
		const mockLikes = Array.from({ length: 10 }, (_, i) => {
			const id = i + 1 + (page.value - 1) * 10;
			const hasImage = Math.random() > 0.4;
			
			return {
				id: id,
				likeTime: `${Math.floor(Math.random() * 24) + 1}小时前`,
				user: {
					id: Math.floor(Math.random() * 100) + 1,
					nickname: `用户${Math.floor(Math.random() * 100) + 1}`,
					avatar: `https://picsum.photos/100?user${Math.floor(Math.random() * 100) + 1}`
				},
				post: {
					id: Math.floor(Math.random() * 50) + 1,
					title: Math.random() > 0.3 ? `我的帖子 #${Math.floor(Math.random() * 50) + 1}` : '',
					content: `这是我发布的帖子内容，分享了一些有趣的事情和想法。这是第${Math.floor(Math.random() * 50) + 1}条内容，感谢点赞支持！`,
					cover: hasImage ? `https://picsum.photos/400/300?post${Math.floor(Math.random() * 100) + 1}` : ''
				}
			};
		});
		
		// 模拟没有更多数据
		if (page.value >= 3) {
			hasMore.value = false;
		}
		
		if (isRefresh) {
			receivedLikesList.value = mockLikes;
			isRefreshing.value = false;
		} else {
			receivedLikesList.value = [...receivedLikesList.value, ...mockLikes];
		}
		
		isLoading.value = false;
	}, 1000);
};

// 加载更多
const loadMore = () => {
	if (!hasMore.value || isLoading.value) return;
	page.value++;
	loadReceivedLikesList();
};

// 下拉刷新
const onRefresh = () => {
	isRefreshing.value = true;
	loadReceivedLikesList(true);
};

// 跳转到用户个人页
const goToUserProfile = (userId) => {
	uni.navigateTo({
		url: `/pages/user/profile?id=${userId}`
	});
};

// 跳转到帖子详情
const goToPostDetail = (postId) => {
	uni.navigateTo({
		url: `/pages/post/detail?id=${postId}`
	});
};

// 返回上一页
const goBack = () => {
	uni.navigateBack({
		delta: 1,
		fail: () => {
			uni.switchTab({
				url: '/pages/user/me'
			});
		}
	});
};

// 去创建帖子
const goToCreatePost = () => {
	uni.navigateTo({
		url: '/pages/post/create'
	});
};

// 页面加载时获取数据
onMounted(() => {
	loadReceivedLikesList();
});
</script>

<style>
.received-likes-page {
	background-color: #f8f8f8;
	min-height: 100vh;
}

.likes-list {
	height: calc(100vh - 88rpx);
	box-sizing: border-box;
}

.list-content {
	padding: 20rpx 30rpx;
	box-sizing: border-box;
}

.like-item {
	margin-bottom: 30rpx;
	background-color: #ffffff;
	padding: 36rpx 30rpx;
	border-radius: 16rpx;
	box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
	box-sizing: border-box;
}

.like-header {
	display: flex;
	align-items: center;
	margin-bottom: 20rpx;
}

.user-info {
	display: flex;
	align-items: center;
	flex: 1;
}

.user-avatar {
	width: 90rpx;
	height: 90rpx;
	border-radius: 50%;
	margin-right: 20rpx;
}

.user-detail {
	display: flex;
	flex-direction: column;
}

.user-name {
	font-size: 32rpx;
	color: #333;
	font-weight: 500;
	margin-bottom: 8rpx;
}

.like-time {
	font-size: 26rpx;
	color: #999;
}

.post-content {
	display: flex;
	background-color: #f8f8f8;
	border-radius: 12rpx;
	padding: 24rpx;
	margin-top: 10rpx;
}

.post-info {
	flex: 1;
	margin-right: 20rpx;
}

.like-desc {
	font-size: 28rpx;
	color: #666;
	margin-bottom: 12rpx;
	display: block;
}

.post-title {
	font-size: 32rpx;
	font-weight: 600;
	color: #333;
	margin-bottom: 12rpx;
	display: block;
}

.post-brief {
	font-size: 28rpx;
	color: #666;
	line-height: 1.6;
	display: -webkit-box;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 2;
	overflow: hidden;
}

.post-cover {
	width: 160rpx;
	height: 160rpx;
	border-radius: 8rpx;
	object-fit: cover;
}

.empty-state {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	padding-top: 240rpx;
	padding-bottom: 100rpx;
	width: 100%;
	box-sizing: border-box;
	padding-left: 30rpx;
	padding-right: 30rpx;
}

.empty-text {
	font-size: 34rpx;
	color: #999;
	margin: 40rpx 0;
}

.create-btn {
	background-color: #0078ff;
	color: white;
	font-size: 32rpx;
	padding: 20rpx 40rpx;
	border-radius: 100rpx;
	border: none;
	width: 400rpx;
	font-weight: 500;
	box-shadow: 0 8rpx 20rpx rgba(0, 120, 255, 0.2);
}

.loading-more, .no-more {
	text-align: center;
	padding: 40rpx 0;
	color: #999;
	font-size: 28rpx;
	width: 100%;
	box-sizing: border-box;
}
</style> 